package com.lanou3g.dllo.baidumusic.tools;

/**
 * Created by dllo on 16/6/22.
 */

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.dllo.baidumusic.base.MyApp;

/**
 * Created by dllo on 16/6/20.
 */

public class MyVolley {

    private static MyVolley myVolley;

    private RequestQueue queue = SingleVolley.getSingleVolley(MyApp.context).getRequestQueue();

    public MyVolley() {

    }

    public static MyVolley getMyVolley() {
        if (myVolley == null) {
            synchronized (MyVolley.class) {
                if (myVolley == null) {
                    myVolley = new MyVolley();
                }
            }
        }
        return myVolley;
    }

//    public void initVolley(String url, final VolleyGetResponse VolleyGetResponse) {
//        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
//            @Override
//            public void onResponse(String response) {
//                VolleyGetResponse.getResponse(response);
//            }
//        }, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {

//            }
//        });
//        queue.add(request);
//    }

    public <T> void initGson(String url, final GetGsonListener<T> listener, final Class<T> clazz) {
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response.substring(0, 1).equals("(")) {
                    response = response.substring(1, response.length() - 2);
                }
                Gson gson = new Gson();
                listener.getGsonListener(gson.fromJson(response, clazz));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        queue.add(request);
    }

    public interface GetGsonListener<T> {
        void getGsonListener(T data);
    }

}